package com.zhihu_lvliangi_android.option.zhengwu.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.android.volley.Response.Listener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.domain.JsonPageModel;
import com.zhihu_lvliangi_android.domain.ZhengwuDepartColumnItem;
import com.zhihu_lvliangi_android.option.parent.ParentActivity;
import com.zhihu_lvliangi_android.option.parent.ParentFragment;
import com.zhihu_lvliangi_android.option.zhengwu.Category_Item_List_Activity;
import com.zhihu_lvliangi_android.option.zhengwu.adapter.ColumnAnotherGridViewAdapter;
import com.zhihu_lvliangi_android.service.DataFormat;
import com.zhihu_lvliangi_android.service.MobileApplication;
import com.zhihu_lvliangi_android.service.TaskID;
import com.zhihu_lvliangi_android.util.Constant;
import com.zhihu_lvliangi_android.util.RequestURL;

public class Category_Department extends ParentFragment{
	
	private List<ZhengwuDepartColumnItem> mColumnItems = new ArrayList<ZhengwuDepartColumnItem>();
	JsonPageModel<ZhengwuDepartColumnItem>	jsonItems_1 =new JsonPageModel<ZhengwuDepartColumnItem>();
	private ColumnAnotherGridViewAdapter mAdapter;
	public static Category_Department newInstance() {
		return new Category_Department();
	}
	
	@Override
	protected int getLayoutId() {
		return R.layout.fragment_theme_subitems;
	}
	@Override
	protected void setupViews(View parentView) {
		parentView.findViewById(R.id.main_dialog_frame).setBackgroundResource(R.color.white);
		Gson gson = new Gson();
		String jsoString = MobileApplication.cacheUtils.getAsString(Constant.CACHE_ZHENGWU_OBJ_DEPART);
		if (TextUtils.isEmpty(jsoString)) {
			Log.e(MobileApplication.TAG, "部门分类需要请求网络数据");
//			jsoString = getResources().getString(R.string.jon_depart);
			MobileApplication.getInstance().clientTask.executeGet(TaskID.TASK_APP_ZHENGWU_COLUM_INIT_3
					, DataFormat.STRING, RequestURL.getGuideTypesForDepartment(), this);
			MobileApplication.getInstance().clientTask.cancelLoadingView();
		}else {
			jsonItems_1 = gson.fromJson(jsoString,new TypeToken<JsonPageModel<ZhengwuDepartColumnItem>>(){}.getType() );
			mColumnItems = jsonItems_1.getResult().getRows();
		}
			
		GridView gridView = (GridView) parentView.findViewById(R.id.theme_items_gridview);
		mAdapter= new ColumnAnotherGridViewAdapter(getActivity(),mColumnItems);
		gridView.setAdapter(mAdapter);
		gridView.setOnItemClickListener(itemClickListener);
	}
	@Override
	protected void initialized(Bundle savedInstanceState) {
		
	}
	@Override
	public void threadTask() {
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				if (mColumnItems.size()>1 &&TextUtils.isEmpty(MobileApplication.cacheUtils.getAsString(mColumnItems.get(0).getGuide_depart_id()))) {
					for (ZhengwuDepartColumnItem item : jsonItems_1.getResult().getRows()) {
						MobileApplication.getInstance().clientTask.executeGet(TaskID.TASK_APP_ZHENGWU_GUIDE_ITEM, DataFormat.STRING, 
								RequestURL.getGuideItemByDepartmentID(item.getGuide_depart_id()), new MyListener(item.getGuide_depart_id()));
					}
				}
			}
				
		}, 500);
		
		
	}
	
	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			//查询
			try {
				
				String jsonString =MobileApplication.cacheUtils.getAsString(mColumnItems.get(position).getGuide_depart_id());
				if (TextUtils.isEmpty(jsonString)) {
					jsonString = getResources().getString(R.string.json_item);
				}
				Intent intent = new Intent(getActivity(), Category_Item_List_Activity.class);
				intent.putExtra(Constant.GENERAL_SAVE_KEY, jsonString);
				intent.putExtra("URL", RequestURL.getGuideItemByDepartmentID(mColumnItems.get(position).getGuide_depart_id()));
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	class MyListener implements Listener<Object>{

		private String mID;
		public MyListener(String id){
			
			this.mID = id;
		}
		@Override
		public void onResponse(Object response, Enum<?> reqId) {
			System.out.println("MyListener ="+ mID+"====================\r\n"+response);
			MobileApplication.cacheUtils.put(mID, response.toString());
		}
		
	}
	
	@Override
	public void onResponse(Object response, Enum<?> reqId) {
		super.onResponse(response, reqId);
		MobileApplication.cacheUtils.put(Constant.CACHE_ZHENGWU_OBJ_DEPART, response.toString());
		jsonItems_1 = gson.fromJson(response.toString(),new TypeToken<JsonPageModel<ZhengwuDepartColumnItem>>(){}.getType() );
		mColumnItems = jsonItems_1.getResult().getRows();
		mAdapter.setColumnItems(mColumnItems);
		mAdapter.notifyDataSetChanged();
	}
	
}
